package com.christian.electronic.invoice.controller;

import com.christian.electronic.invoice.service.CreateElectronicInvoiceService;
import com.christian.electronic.invoice.service.DownloadInvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 文件说明：电子发票接口
 *
 * @Author wang
 * @Date 2019/9/27 11:21
 */
@Slf4j
@RestController
@RequestMapping("/invoice")
public class ElectronicInvoiceController {

    @Autowired
    private CreateElectronicInvoiceService electronicInvoiceService;

    @Autowired
    private DownloadInvoiceService downloadInvoiceService;

    /**
     * 文件下载
     *
     * @param orderNumber
     * @return
     */
    @GetMapping("/download/{orderNumber}")
    public String downloadInvoice(@PathVariable String orderNumber, HttpServletResponse response) {
        //生成电子发票文件
        boolean result = electronicInvoiceService.createElectronicInvoice(orderNumber);
        //判断生成结果
        if (!result) {
            return "生成电子发票失败！";
        }

        //下载文件
        boolean flag = downloadInvoiceService.downloadInvoice(orderNumber, response);
        //判断文件是否下载成功
        if (!flag) {
            return "文件下载失败！";
        }

        return "文件下载成功！";
    }
}
